#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#


from supercolumn import SuperColumn

from jutebag import phpserialize


class   ColumnFamily(object) :
    '''class ColumnFamily
    '''

    # column types
    COLUMN_TYPE_SIMPLE = 'SIMPLE'
    COLUMN_TYPE_SUPER  = 'SUPER'

    # index types
    SORT_BY_NAME = 'NAME'
    SORT_BY_TIME = 'TIME'


    def __init__(self, name=None, column_type = COLUMN_TYPE_SIMPLE) :
        # name of column family
        self._name = name
        # type of column in this column-family
        self._column_type = column_type

        self._local_delete_time = 0
        self._marked_for_delete_at = 0

        # columns 
        self._columns = {}
        ## __init__()

    
    ####-----------------------------------------------------------####

    def getName(self) :
        return  self._name
        ## name()
    name = property(getName,)

    def isSuper(self) :
        return self._column_type == self.COLUMN_TYPE_SUPER
        ## isSuper()

    
    def getColumn(self, column_name) :
        if not isinstance(column_name, str) :
            raise TypeError, '<Type.str> is required.'
        if not column_name :
            return None
        if column_name in self._columns :
            return self._columns[ column_name ]
        return None
        ## getColumn()

    def addColumns(self, columns) :
        raise RuntimeError, 'Not Implemented, yet'
        ## addColumns()

    def addColumnFamily(self, columns) :
        raise RuntimeError, 'Not Implemented, yet'
        ## addColumns()

    def addColumn(self, column) :
        name = column.name
        
        if name in self._columns :
            old_column = self._columns[ name ]
            if isinstance(old_column, SuperColumn) :
                raise NotImplementedError
            else :
                if old_column.comparePriority(column) <= 0 :
                    self._columns[ name ] = column
        else :
            self._columns[ name ] = column
        ## addColumn()


    def getMarkedForDeleteAt(self) :
        return  self._marked_for_delete_at

    def getLocalDeletionTime(self) :
        return  self._local_delete_time

    def markForDeleteAt(self, local_delete_time, timestamp) :
        self._local_delete_time = local_delete_time
        self._marked_for_delete_at = timestamp

    def isMarkedForDelete(self) :
        return self._marked_for_delete_at != 0


    def getColumnCount(self) :
        count = 0
        for name,column in self._columns.iteritems() :
            count += column.getColumnCount()

        return count
        ## getColumnCount()

    def getColumnSize(self) :
        size = 0
        for name,column in self._columns.iteritems() :
            size += column.getColumnSize()

        return size
        ## getColumnSize()

    ####-----------------------------------------------------------####

    class   Serializer(object) :

        @staticmethod
        def serialize(column_family) :
            '''serialize column_family into stream
            '''
            return phpserialize.serialize(column_family)
            ## serialize()

        @staticmethod
        def deserialize(column_family_serialized) :
            '''deserialize column_family
            '''
            o = phpserialize.unserialize(column_family_serialized)
            return o
            ## deserialize()

        ## class Serializer()


    ## class ColumnFamily()

